﻿using Infrastructure;
using Lan.Dto;
using Lan.Model;
using Lan.Repository;
using Lan.ServiceCore.IService;
using Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lan.ServiceCore.Services
{
    [AppService(ServiceType = typeof(ICameraService), ServiceLifetime = LifeTime.Transient)]
    public class CameraService : Repository<CameraModel>, ICameraService
    {

        public List<CameraModel> GetList(CameraQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var exp = Expressionable.Create<CameraModel>();
            exp.AndIF(!string.IsNullOrEmpty(parm.ip), u => u.Ip.Contains(parm.ip));

            var response = Queryable()
                .Where(exp.ToExpression()).ToList();

            return response;
        }

        private static Expressionable<CameraModel> QueryExp(CameraQueryDto parm)
        {
            var predicate = Expressionable.Create<CameraModel>();

            return predicate;
        }

        public CameraModel GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();
            return response;
        }

        public CameraModel AddCamera(CameraModel model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        public int UpdateCamera(CameraModel model)
        {
            return Update(model, true);
        }
        public int DeleteCamera(object id)
        {
            return Delete(id);
        }

        public List<CameraModel> GetAllList()
        {
            return Queryable()
               // .Where(x => x.Status == 1)
                .ToList();
        }

        public List<CameraModel> SelectCameraIds(int BindingAreaId)
        {
            return Queryable()
                .Where(r => r.BindingAreaId == BindingAreaId)
                .ToList();
        }
        public int unUpdateBindCamera(int BindingAreaId)
        {
            var result = Update(w => w.BindingAreaId == BindingAreaId, it => new CameraModel()
            {
                BindingAreaId = -1
            });
            return result;
        }
        public int UpdateBindCamera(DefenceareaModel defence, int id)
        {
            if (defence.CameraIds == null) return 0;

            var result = 0;
            foreach (var item in defence.CameraIds)
            {
                result = Update(w => w.Id == item, it => new CameraModel()
                {
                    BindingAreaId = id
                });
            }
            return result;
        }
        public void ShowCamera(int Id)
        {

        }
            public CameraModel GetCameraByDefenceAreaId(int DefenceAreaId)
        {
            var response = Queryable()
                .Where(x => x.BindingAreaId == DefenceAreaId)
                .First();

            return response;
        }
    }
}
